<template>
  <div class="login">
    <el-dialog
      title="登录"
      :visible.sync="dialogVisible"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      dialogVisible: true, // 模态框
      form: {
        // 表单
        username: "admin",
        password: "123456",
      },
      rules: {
        // 表单验证
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      //登录
      // console.log(this.$refs);
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$api.Login(this.form).then((res) => {
            console.log(res);
            if (res.data.code == 200) {
              sessionStorage.setItem("token", res.data.token); //存token
              sessionStorage.setItem("userinfo", JSON.stringify(res.data.data)); //存用户信息
              this.$router.replace("/menu"); //跳路由
            }
          });
        } else {
          alert("输入错误");
        }
      });
    },
    resetForm() {
      //重置
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style>
</style>